package top.mybatisx.sql.core.sql;

import top.mybatisx.sql.core.sql.base.BaseSql;
import top.mybatisx.sql.core.toolkit.ChkUtil;

/**
 * 查询
 */
public interface Where<T extends Condition> extends BaseSql {

    StringBuilder whereSql = new StringBuilder();

    default T where() {
        isWhere.flag = 1;

        return (T) this;
    }

    default T where(String sql) {
        this.where();

        whereSql.setLength(0);
        whereSql.append(sql);
        return (T) this;
    }

    default String buildWhereSql() {
        if (whereSql.length() > 0 || ChkUtil.isNull(whereList)) {
            // 如果手工设置了 sql, 直接返回
            return whereSql.toString();
        }

        whereSql.setLength(0);
        whereSql.append(" where ");

        String allCondSql = getAllCondSql(whereList);
        whereSql.append(allCondSql);

        return whereSql.toString();
    }


}
